import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/regist',
    component: () => import('@/views/regist/index'),
    hidden: true
  },
  {
    path: '/retrievePWD',
    component: () => import('@/views/regist/retrievePWD'),
    hidden: true
  },
  {
    path: '/retrievePWD2',
    component: () => import('@/views/regist/retrievePWD2'),
    hidden: true
  },
  {
    path: '/retrievePWD3',
    component: () => import('@/views/regist/retrievePWD3'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

export const asyncRoutes = [
  /* {
    path: '/book',
    component: Layout,
    redirect: '/book/list',
    name: 'Book',
    meta: {
      title: '图书管理',
      icon: 'documentation',
      roles: ['admin', 'editor'] 
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/book/list'),
        name: 'BookList',
        meta: { title: '图书列表', icon: 'list', noCache: true, roles: ['admin', 'editor']  }
      },
      {
        path: 'create',
        component: () => import('@/views/book/create'),
        name: 'CreateBook',
        meta: { title: '添加图书', icon: 'edit', noCache: true, roles: ['admin', 'editor']  }
      },
      {
        path: 'edit/:fileName(\\w+)',
        component: () => import('@/views/book/edit'),
        name: 'EditBook',
        meta: { title: '编辑图书', noCache: true, activeMenu: '/book/list', roles: ['admin', 'editor'] },
        hidden: true
      }
    ]
  }, */
  /* {
    path: '/news',
    name: 'news',
    component: Layout,
    redirect: '/news/create',
    meta: { title: '新闻管理', icon: 'documentation', roles: ['admin', 'editor'] },
    children: [
      {
        name: 'newsList',
        path: '/news/list',
        component: () => import('@/views/news/list'),
        meta: { title: '新闻列表', icon: 'list', roles: ['admin'] }
      },
      {
        name: 'newsCreate',
        path: '/news/create',
        component: () => import('@/views/news/create'),
        meta: { title: '上传新闻', icon: 'edit', roles: ['admin'] }
      },
      {
        name: 'newsEdit',
        path: '/news/edit/:fileName',
        component: () => import('@/views/news/edit'),
        hidden: true,
        meta: { title: '编辑新闻', icon: 'edit', roles: ['admin'], activeMenu: '/news/list' }
      }
    ]
  }, */
  {
    path: '/enroll',
    name: 'enroll',
    component: Layout,
    redirect: '/enroll/create',
    meta: { title: '报名信息', icon: 'documentation', roles: ['admin','user'] },
    children: [
      {
        name: 'enrollList',
        path: '/enroll/list',
        component: () => import('@/views/enroll/list'),
        meta: { title: '查看和修改报名信息', icon: 'list', roles: ['user'] }
      },
     /* {
        name: 'enrollUserList',
        path: '/enroll/listUser',
        component: () => import('@/views/enroll/listUser'),
        meta: { title: '报名列表', icon: 'list', roles: ['user'] }
      }, */
      {
        name: 'baoming',
        path: '/enroll/baoming',
        component: () => import('@/views/enroll/baoming'),
        meta: { title: '开始报名', icon: 'edit', roles: ['user'] }
      },
      {
        name: 'create',
        path: '/enroll/create/:fileName',
        component: () => import('@/views/enroll/create'),
        hidden: true,
        meta: { title: '填写报名', icon: 'edit', roles: ['user'] }
      },
      {
        name: 'enrollEdit',
        path: '/enroll/edit/:fileName',
        component: () => import('@/views/enroll/edit'),
        hidden: true,
        meta: { title: '修改报名表', icon: 'edit', activeMenu: '/enroll/list', roles: ['user']}
      },
      {
        name: 'enrollView',
        path: '/enroll/enrollview/:fileName',
        component: () => import('@/views/enroll/enrollview'),
        hidden: true,
        meta: { title: '查看报名表', icon: 'edit', activeMenu: '/enroll/list', roles: ['user']}
      }
    ]
  },
  {
    path: '/user',
    name: 'user',
    component: Layout,
    redirect: '/user/create',
    meta: { title: '用户管理', icon: 'documentation', roles: ['admin', 'user'] },
    children: [
      {
        name: 'userList',
        path: '/user/list',
        component: () => import('@/views/user/list'),
        meta: { title: '用户信息', icon: 'list', roles: ['admin', 'user'] }
      },
      {
        name: 'userCreate',
        path: '/user/create',
        component: () => import('@/views/user/create'),
        meta: { title: '添加用户', icon: 'edit', roles: ['admin'] }
      },
      {
        name: 'userEdit',
        path: '/user/edit/:fileName',
        component: () => import('@/views/user/edit'),
        hidden: true,
        meta: { title: '编辑用户', icon: 'edit', activeMenu: '/user/list', roles: ['admin', 'user'],}
      },
      {
        name: 'userPasswordEdit',
        path: '/user/password/:fileName',
        component: () => import('@/views/user/password'),
        hidden: true,
        meta: { title: '修改密码', icon: 'edit',  activeMenu: '/user/password', roles: ['admin', 'user'],}
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
